package com.inews.business.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.inews.base.exception.BusinessException;
import com.inews.base.mapper.BeanMapper;
import com.inews.base.persistence.DynamicSpecifications;
import com.inews.base.persistence.SearchFilter;
import com.inews.base.security.vo.BaseUserVo;
import com.inews.base.utils.ExtDataUtil;
import com.inews.base.utils.InewsUtils;
import com.inews.base.vo.ExtData;
import com.inews.business.constants.BatchType;
import com.inews.business.constants.InewsBusinessConstants;
import com.inews.business.entity.TblBatchLog;
import com.inews.business.entity.TblRetireStaff;
import com.inews.business.entity.TblRetireStaffHis;
import com.inews.business.entity.TblSysUser;
import com.inews.business.entity.TblTeacher;
import com.inews.business.entity.TblTeacherHis;
import com.inews.business.repository.BatchLogDao;
import com.inews.business.repository.ITeacherDao;
import com.inews.business.repository.RetireStaffDao;
import com.inews.business.repository.RetireStaffHisDao;
import com.inews.business.repository.TeacherHisDao;
import com.inews.business.vo.BatchLogVo;

@Service
@Transactional(readOnly=true)
public class BatchLogServiceImpl implements BatchLogService, InewsBusinessConstants{

	@Autowired
	private BatchLogDao batchLogDao;
	@Autowired
	private BaseService baseService;
	@Autowired
	private ITeacherDao teacherDao;
	@Autowired
	private TeacherHisDao teacherHisDao;
	@Autowired
	private RetireStaffDao retireStaffDao;
	@Autowired
	private RetireStaffHisDao retireStaffHisDao;
	
	/**
	 * 分页查询批处理信息.
	 *
	 * @param filterParams the filter params
	 * @param page the page
	 * @return the ext data
	 */
	public ExtData<BatchLogVo> findBatchLogPage(Map<String, Object> filterParams, Pageable page){
		List<BatchLogVo> voList = new ArrayList<BatchLogVo>();
		Map<String, SearchFilter> filters = SearchFilter.parse(filterParams);
		Page<TblBatchLog> pagePoList = batchLogDao.findAll(DynamicSpecifications.bySearchFilter(filters.values(), TblBatchLog.class), page);
		for (TblBatchLog po : pagePoList.getContent()) {
			BatchLogVo vo = BeanMapper.map(po, BatchLogVo.class);
			vo.setBatchDateFormat(InewsUtils.fomatDate(po.getBatchDate()));
			vo.setBatchTypeName(BatchType.getName(po.getBatchType()));
			vo.setLastUpdUserName(po.getLastUpdUser().getRealName());
			vo.setLastUpdTimeFormat(InewsUtils.fomatLongDate(po.getLastUpdTime()));
			voList.add(vo);
		}
		ExtData<BatchLogVo> extData = ExtDataUtil.genWithPagingList(voList, pagePoList.getTotalElements());
		return extData;
	}
	
	
	
	/**
	 * 执行批处理.
	 *
	 * @param user the user
	 * @return the ext data
	 */
	public ExtData<String> executeBatch(BaseUserVo user) {
		this.batchTeacher(user);
		this.batchRetireStaff(user);
		return ExtDataUtil.genWithSingleData("执行批处理成功");
	}
	
	
	
	/**
	 * 执行教师批处理.
	 *
	 * @param user the user
	 * @return the ext data
	 */
	private ExtData<String> batchTeacher(BaseUserVo user){
		String currentTime = baseService.getCurrentTime();
		TblSysUser editUser = baseService.getEditUser(user);
		String batchDate = currentTime.substring(0, 6) + "01";
		List<TblBatchLog> existList = batchLogDao.findByBatchDateAndBatchType(batchDate, BATCH_TYPE_NORMAL);
		if(InewsUtils.isNotEmpty(existList)){
			throw new BusinessException("本月已经执行了该批处理");
		}
		Iterable<TblTeacher> pos = teacherDao.findAll();
		if(InewsUtils.isNotEmpty(pos)){
			for (TblTeacher po : pos) {
				TblTeacherHis hisPo = BeanMapper.map(po, TblTeacherHis.class);
				hisPo.setBatchDate(batchDate);
				teacherHisDao.save(hisPo);
			}
		}
		TblBatchLog batchLogPo = new TblBatchLog();
		batchLogPo.setBatchDate(batchDate);
		batchLogPo.setBatchType(BATCH_TYPE_NORMAL);
		batchLogPo.setCreateTime(currentTime);
		batchLogPo.setCreateUser(editUser);
		batchLogPo.setLastUpdUser(editUser);
		batchLogPo.setLastUpdDes(LAST_UPD_DES_ADD);
		batchLogPo.setLastUpdTime(currentTime);
		batchLogDao.save(batchLogPo);
		return ExtDataUtil.genWithSingleData("执行教师批处理成功");
	}
	
	
	
	/**
	 * 执行退休教师批处理.
	 *
	 * @param user the user
	 * @return the ext data
	 */
	private ExtData<String> batchRetireStaff(BaseUserVo user){
		String currentTime = baseService.getCurrentTime();
		TblSysUser editUser = baseService.getEditUser(user);
		String batchDate = currentTime.substring(0, 6) + "01";
		List<TblBatchLog> existList = batchLogDao.findByBatchDateAndBatchType(batchDate, BATCH_TYPE_RETIRE);
		if(InewsUtils.isNotEmpty(existList)){
			throw new BusinessException("本月已经执行了该批处理");
		}
		Iterable<TblRetireStaff> pos = retireStaffDao.findAll();
		if(InewsUtils.isNotEmpty(pos)){
			for (TblRetireStaff po : pos) {
				TblRetireStaffHis hisPo = BeanMapper.map(po, TblRetireStaffHis.class);
				hisPo.setBatchDate(batchDate);
				hisPo.setCreateTime(currentTime);
				hisPo.setCreateUser(editUser);
				hisPo.setLastUpdUser(editUser);
				hisPo.setLastUpdDes(LAST_UPD_DES_ADD);
				hisPo.setLastUpdTime(currentTime);
				retireStaffHisDao.save(hisPo);
			}
		}
		TblBatchLog batchLogPo = new TblBatchLog();
		batchLogPo.setBatchDate(batchDate);
		batchLogPo.setBatchType(BATCH_TYPE_RETIRE);
		batchLogPo.setCreateTime(currentTime);
		batchLogPo.setCreateUser(editUser);
		batchLogPo.setLastUpdUser(editUser);
		batchLogPo.setLastUpdDes(LAST_UPD_DES_ADD);
		batchLogPo.setLastUpdTime(currentTime);
		batchLogDao.save(batchLogPo);
		return ExtDataUtil.genWithSingleData("执行退休教师批处理成功");
	}



}
